package john.wenzi.server.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import john.wenzi.server.dao.FileUploadDao;
import john.wenzi.server.entity.FileUploadEntity;
import john.wenzi.server.filter.PermissionCheckFilter;
import john.wenzi.server.utils.JdbcUtil;

public class FileUploadDaoImpl implements FileUploadDao{

	private JdbcUtil jdbcUtil = new JdbcUtil();
	public static Logger logger = LoggerFactory.getLogger(FileUploadDaoImpl.class);
	
	@Override
	public void insertUploadRecord(FileUploadEntity fileUploadEntity) throws Exception {
		logger.info("上传文件信息插入数据库开始");
		Connection conn = jdbcUtil.getConnection();
		Statement stat = conn.createStatement();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql = "INSERT INTO upload_record VALUES('"+fileUploadEntity.getUuidName()+"',"
				+ "'"+fileUploadEntity.getFileSize()+"',"
				+ "'"+fileUploadEntity.getFileType()+"',"
				+ "'"+fileUploadEntity.getFileName()+"',"
				+ "'"+sdf.format(fileUploadEntity.getCreateDate())+"',"
				+ "'"+fileUploadEntity.getFilePath()+"',"
				+ "'"+fileUploadEntity.getEncryptDigitalEnvelope()+"',"
				+ "'"+fileUploadEntity.getStatus()+"')";
		stat.executeUpdate(sql);
		logger.info("上传文件信息插入数据库语句："+sql);
		jdbcUtil.release(null, stat, conn);
		logger.info("上传文件信息插入数据库结束");
	}

	@Override
	public FileUploadEntity findUploadRecord(String uuidName) throws Exception {
		logger.info("从数据库查询文件信息开始");
		Connection conn = jdbcUtil.getConnection();
		Statement stat = conn.createStatement();
		FileUploadEntity fileUploadEntity = new FileUploadEntity();
		String sql = "SELECT * FROM upload_record WHERE uuid_name = '"+uuidName+"' AND status = '1'";
		logger.info("从数据库查询文件信息语句："+sql);
		ResultSet result = stat.executeQuery(sql);
		if(result != null) {
			while(result.next()) { 
				fileUploadEntity.setUuidName(result.getString("uuid_name"));
				fileUploadEntity.setFileSize(result.getString("file_size"));
				fileUploadEntity.setFileType(result.getString("file_type"));
				fileUploadEntity.setFileName(result.getString("file_name"));
				fileUploadEntity.setCreateDate(result.getTimestamp("create_date"));
				fileUploadEntity.setFilePath(result.getString("file_path"));
				fileUploadEntity.setStatus(result.getString("status"));
				fileUploadEntity.setEncryptDigitalEnvelope(result.getString("encrypt_digital_envelope"));
			}
		}
		jdbcUtil.release(null, stat, conn);
		logger.info("从数据库查询文件信息结果："+fileUploadEntity.toString());
		return fileUploadEntity;
	}

	@Override
	public List<FileUploadEntity> findAllUploadRecord() throws Exception {
		logger.info("从数据库查询文件上传记录开始");
		Connection conn = jdbcUtil.getConnection();
		Statement stat = conn.createStatement();
		ArrayList<FileUploadEntity> list = new ArrayList<FileUploadEntity>();
		String sql = "select * from upload_record order by create_date desc offset 0 rows fetch next 10 rows only";
		logger.info("从数据库查询文件上传记录语句："+sql);
		ResultSet result = stat.executeQuery(sql);
		if(result != null) {
			while(result.next()) { 
				FileUploadEntity fileUploadEntity = new FileUploadEntity();
				fileUploadEntity.setUuidName(result.getString("uuid_name"));
				fileUploadEntity.setFileSize(result.getString("file_size"));
				fileUploadEntity.setFileType(result.getString("file_type"));
				fileUploadEntity.setFileName(result.getString("file_name"));
				fileUploadEntity.setCreateDate(result.getTimestamp("create_date"));
				fileUploadEntity.setFilePath(result.getString("file_path"));
				fileUploadEntity.setStatus(result.getString("status"));
				fileUploadEntity.setEncryptDigitalEnvelope(result.getString("encrypt_digital_envelope"));
				list.add(fileUploadEntity);
			}
		}
		jdbcUtil.release(null, stat, conn);
		logger.info("从数据库查询文件上传记录结果："+list.toString());
		return list;
		
	}

}
